package com.link.base.base.redpacketconfig.service;

import javax.annotation.Resource;

import com.link.base.base.member.util.MpPayConstants;
import com.link.base.base.redpacketconfig.model.RedPktCfg;
import com.link.base.base.redpacketconfig.model.RedPktDrawCfg;
import com.link.base.base.redpacketconfig.model.RedPktGlobalCfg;
import com.link.base.base.redpacketconfig.model.RedPktPayCfg;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.RedisUtil;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;
import com.link.base.base.redpacketconfig.dao.mybatis.mapper.RedPktGlobalCfgMapper;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;


import java.util.List;

/**
 * @author: 闫有霞
 * @date: 2019-01-23
 * @comments: 红包全局设置
 */
@Service
public class RedPktGlobalCfgServiceImpl extends BasicServiceImpl<RedPktGlobalCfg> implements RedPktGlobalCfgService {

    @Resource
    private RedPktGlobalCfgMapper redPktGlobalCfgMapper;

    @Resource
    private RedPktDrawCfgService redPktDrawCfgService;

    @Resource
    private RedPktPayCfgService redPktPayCfgService;

    @Resource
    private KeyGenerateService keyGenerateService;





    @Override
    public BasicMapper<RedPktGlobalCfg> getBasicMapper() {
        return redPktGlobalCfgMapper;
    }


    /**
     *@createdBy 闫有霞
     *@date 2019/1/23
     *@methods: loadGlobalCfgToRedis
     *@param:   RedPktGlobalCfg entity
     *@return:  void
     *@description: 加载全局设置数据到Redis
     **/
    @Override
    public void loadGlobalCfgToRedis(RedPktGlobalCfg entity) throws Exception {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            /**领取失败*/
            //限制时间
            jedis.hset(MpPayConstants.FAIL_TO_RECIEVE, MpPayConstants.FAIL_TO_RECIEVE_LIMIT_TIME, String.valueOf(entity.getDrawFailTime()));
            //限制数量
            jedis.hset(MpPayConstants.FAIL_TO_RECIEVE, MpPayConstants.FAIL_TO_RECIEVE_LIMIT_NUM, String.valueOf(entity.getDrawFailNum()));
            //通知手机号
            jedis.hset(MpPayConstants.FAIL_TO_RECIEVE, MpPayConstants.FAIL_TO_RECIEVE_ALERT_PHONE_NUMBER, entity.getFailNotifyPhones());
            // 短信模板
            jedis.hset(MpPayConstants.FAIL_TO_RECIEVE, MpPayConstants.FAIL_TO_RECIEVE_SMS_CODE, entity.getMsgParams());

            /**领取请求*/
            //限制时间
            jedis.hset(MpPayConstants.OVER_FREQUENCY_REQUEST, MpPayConstants.OVER_FREQUENCY_REQUEST_LIMIT_TIME,
                    String.valueOf(entity.getDrawReqTime()));
            //限制数量
            jedis.hset(MpPayConstants.OVER_FREQUENCY_REQUEST, MpPayConstants.OVER_FREQUENCY_REQUEST_LIMIT_NUM,
                    String.valueOf(entity.getDrawReqNum()));
            //通知手机号
            jedis.hset(MpPayConstants.OVER_FREQUENCY_REQUEST, MpPayConstants.OVER_FREQUENCY_REQUEST_ALERT_PHONE_NUMBER, entity.getReqNotifyPhones());
            // 超频短信模板
            jedis.hset(MpPayConstants.OVER_FREQUENCY_REQUEST, MpPayConstants.OVER_FREQUENCY_REQUEST_SMS_CODE, entity.getMsgParams());

            /**伪造请求*/
            //限制时间
            jedis.hset(MpPayConstants.FORGERY_REQUEST, MpPayConstants.FORGERY_REQUEST_LIMIT_TIME, String.valueOf(entity.getFakeReqTime()));
            //限制数量
            jedis.hset(MpPayConstants.FORGERY_REQUEST, MpPayConstants.FORGERY_REQUEST_LIMIT_NUM, String.valueOf(entity.getFakeReqNum()));
            //通知手机号
            jedis.hset(MpPayConstants.FORGERY_REQUEST, MpPayConstants.FORGERY_REQUEST_ALERT_PHONE_NUMBER, entity.getFakeReqNotifyPhones());
            // 伪造请求短信模板
            jedis.hset(MpPayConstants.FORGERY_REQUEST, MpPayConstants.FORGERY_REQUEST_SMS_CODE, entity.getMsgParams());
        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    @Override
    public void insertGlobalCfg(RedPktGlobalCfg entity) throws Exception {
        if (entity != null) {
            Long id = keyGenerateService.keyGenerate();
            Long corpid = UserUtil.getUser().getLoginCorpId();
            entity.setId(id);
            entity.setCorpid(corpid);
            redPktGlobalCfgMapper.insert(entity);
            loadGlobalCfgToRedis(entity);
        }
    }

    @Override
    public void updateGlobalCfg(RedPktGlobalCfg entity) throws Exception {
        if (entity != null) {
            redPktGlobalCfgMapper.update(entity);
            loadGlobalCfgToRedis(entity);
        }
    }

    @Override
    public void insertRedCfg(RedPktCfg entity) throws Exception {
        insertGlobalCfg(entity.getRedGlobalCfg());
        redPktDrawCfgService.insertDrawCfg(entity.getRedDrawCfg());
        redPktPayCfgService.insertPayCfg(entity.getRedPayCfg());
    }

    @Override
    public void updateRedCfg(RedPktCfg entity) throws Exception {
        updateGlobalCfg(entity.getRedGlobalCfg());
        redPktDrawCfgService.updateDrawCfg(entity.getRedDrawCfg());
        redPktPayCfgService.updatePayCfg(entity.getRedPayCfg());
    }

    @Override
    public RedPktCfg queryRedCfg(RedPktCfg entity) throws Exception {
        RedPktCfg redCfg = new RedPktCfg();
        List<RedPktGlobalCfg>   globalCfgList = redPktGlobalCfgMapper.queryAll(entity.getRedGlobalCfg());
        if (globalCfgList.size() > 0) {
            redCfg.setRedGlobalCfg(globalCfgList.get(0));
        }
        List<RedPktDrawCfg> drawCfgList = redPktDrawCfgService.queryAll(entity.getRedDrawCfg());
        if (drawCfgList.size() > 0) {
            redCfg.setRedDrawCfg(drawCfgList.get(0));
        }
        List<RedPktPayCfg> payCfgList = redPktPayCfgService.queryAll(entity.getRedPayCfg());
        if (payCfgList.size() > 0) {
            redCfg.setRedPayCfg(payCfgList.get(0));
        }
        return redCfg;
    }

    @Override
    public void upsertRedCfg(RedPktCfg entity) throws Exception {
        RedPktCfg redCfg = queryRedCfg(entity);
        if (redCfg.getRedGlobalCfg()  !=  null && redCfg.getRedDrawCfg() != null && redCfg.getRedPayCfg() != null) {
            updateRedCfg(entity);
        } else {
            insertRedCfg(entity);
        }

    }
}

